package cn.exam;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Em2 {
    public static void main(String[] args) {
        a(150000);
        b(150000);
    }

    static void a(int n) {
        //实例化 添加元素
        long s = System.currentTimeMillis();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(0, "hello" + i);
        }
        long e = System.currentTimeMillis();
        System.out.printf("ArrayList new add :%d%n", e - s);

        //设置元素
        s = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            list.set(i, "java" + i);
        }
        e = System.currentTimeMillis();
        System.out.printf("ArrayList set :%d%n", e - s);

        //删除元素
        s = System.currentTimeMillis();
        for (int i = 0; i < n / 2; i++) {
            list.remove(0);
        }
        e = System.currentTimeMillis();
        System.out.printf("ArrayList remove :%d%n", e - s);

        //读取元素
        s = System.currentTimeMillis();
        for (int i = 0; i < n / 2; i++) {
            String t = list.get(i);
        }
        e = System.currentTimeMillis();
        System.out.printf("ArrayList read :%d%n", e - s);
    }

    static void b(int n) {
        //实例化 添加元素
        long s = System.currentTimeMillis();
        LinkedList<String> list2 = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            list2.add(0, "hello" + i);
        }
        long e = System.currentTimeMillis();
        System.out.printf("LinkedList new add :%d%n", e - s);

        List<String> list = new ArrayList<>(list2);
        //设置元素
        s = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            list.set(i, "java" + i);
        }
        e = System.currentTimeMillis();
        System.out.printf("LinkedList set :%d%n", e - s);

        //删除元素
        s = System.currentTimeMillis();
        for (int i = 0; i < n / 2; i++) {
            list.remove(0);
        }
        e = System.currentTimeMillis();
        System.out.printf("LinkedList remove :%d%n", e - s);

        //读取元素
        s = System.currentTimeMillis();
        for (int i = 0; i < n / 2; i++) {
            String t = list.get(i);
        }
        e = System.currentTimeMillis();
        System.out.printf("LinkedList read :%d%n", e - s);

        s = System.currentTimeMillis();
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String tt = it.next();
        }
        e = System.currentTimeMillis();
        System.out.printf("LinkedList Iterator read :%d%n", e - s);
    }


}
